package it.unibo.deis.lia.mmhc;

import it.unibo.deis.lia.mmhc.fcm.Connection;
import it.unibo.deis.lia.mmhc.fcm.ConnectionManager;
import it.unibo.deis.lia.mmhc.fcm.Connection.Hop;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;

import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class ConnectionMonitoringGUI extends JFrame implements Runnable{

	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane0;
	private JTree jTree0;
	private JButton jButton0;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public ConnectionMonitoringGUI() {
		initComponents();
	}

	private void initComponents() {
		setLayout(new GroupLayout());
		add(getJScrollPane0(), new Constraints(new Bilateral(4, 0, 22), new Leading(5, 232, 10, 10)));
		add(getJButton0(), new Constraints(new Leading(7, 244, 10, 10), new Leading(251, 10, 10)));
		setSize(258, 284);
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("Refresh");
			jButton0.addMouseListener(new MouseAdapter() {
	
				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}

	private JTree getJTree0() {
		if (jTree0 == null) {
			jTree0 = new JTree();
			DefaultTreeModel treeModel = null;
			{
				DefaultMutableTreeNode node0 = new DefaultMutableTreeNode("Connessioni");
				
					
				
				treeModel = new DefaultTreeModel(node0);
			}
			jTree0.setModel(treeModel);
		}
		return jTree0;
	}

	private JScrollPane getJScrollPane0() {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getJTree0());
		}
		return jScrollPane0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public void run() {
		installLnF();
	
				ConnectionMonitoringGUI frame = new ConnectionMonitoringGUI();
				frame
						.setDefaultCloseOperation(ConnectionMonitoringGUI.EXIT_ON_CLOSE);
				frame.setTitle("ConnectionMonitoring");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocation(300, 300);
				frame.setVisible(true);
				jTree0.setToggleClickCount(1);
				 frame.addWindowListener(new WindowAdapter() {
				        public void windowClosing(WindowEvent event) {
				        	try{
				        		Process k3 = Runtime.getRuntime().exec("sh dis.sh");
				        	
				    		k3.waitFor();
				    		k3.destroy();
				            }
				        	catch (Exception e) {
								// TODO: handle exception
							}
				        }
				    });
				
			
			}
		
	

	private void jButton0MouseMouseClicked(MouseEvent event){
		try {
			ConnectionManager conManager= ConnectionManager.getInstance();
		
		Vector<Connection> conns=conManager.getConnessioni();
		if (jTree0 == null) 
			jTree0 = new JTree();
		DefaultTreeModel treeModel = null;
		{
			DefaultMutableTreeNode node0 = new DefaultMutableTreeNode("Connessioni");
			{for(int i=0;i<conns.size();i++){
				Connection con=conns.elementAt(i);
				DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(con.getRemoteIP()+"-"+con.getLocalIp());
				{
					DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Numero Hop Connessi: "+con.getNumberHop());
					node1.add(node2);
				}
				{
					DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Numero richieste servizi: "+con.getSearchServiceRequestNumber());
					node1.add(node2);
				}
				{
					DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Numero servizi trovati: "+con.getFindedServiceNumber());
					node1.add(node2);
				}
				{
					DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Hop servitori o utilizzatori");
					{
						Vector<Hop> hops=con.getHopService();
						for(int j=0;j<hops.size();j++){
							{
								DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(hops.elementAt(i).getName());
								{
									DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Numero servizi usati: "+hops.elementAt(i).getNumberServiceUsed());
									node3.add(node4);
								}
								{
									DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Numero servizi forniti: "+hops.elementAt(i).getNumberServiceOfferd());
									node3.add(node4);
								}
								node2.add(node3);
							}
						}
					}
					node1.add(node2);
				}
				node0.add(node1);			}
			
			}
			treeModel=new DefaultTreeModel(node0);
		}
		jTree0.setModel(treeModel);
		
		
		
		
		
		
		
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
